package com.totoro.mongo.demo.bulk;

import com.mongodb.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Random;

@Service
public class BulkOption {
    static MongoTemplate mongoTemplate;

    @Resource
    public void initMongoTemplate(MongoTemplate mongoTemplate){
        BulkOption.mongoTemplate = mongoTemplate;
    }

    public static final String COLLECTION_NAME = "user";

    public static BulkWriteOperation getOrderedBulkOperation(){
        return mongoTemplate.getCollection(BulkOption.COLLECTION_NAME).initializeOrderedBulkOperation();
    }

    public static BulkWriteOperation getUnOrderedBulkOperation(){
        return mongoTemplate.getCollection(BulkOption.COLLECTION_NAME).initializeUnorderedBulkOperation();
    }

    /**
     * 批量插入 ordered为true，批量插入时，如果失败，则剩余的document不再插入。ordered为false，插入失败后，剩余的document继续插入
     */
    public void bulkInsert() {
        BulkWriteOperation bulkWriteOperation = mongoTemplate.getCollection(BulkOption.COLLECTION_NAME).initializeOrderedBulkOperation();
        Random age_random = new Random(90);
        for(int i=0; i<100; i++){
            BasicDBObject user = new BasicDBObject().append("name", "user"+i)
                    .append("age", age_random.nextInt(90));
            bulkWriteOperation.insert(user);
        }

        BulkWriteResult result = bulkWriteOperation.execute();
        System.out.println(result.getInsertedCount());
    }

    /**
     * 批量修改
     */
    public void bulkUpdate(){
        BulkWriteOperation bulkWriteOperation = getOrderedBulkOperation();
        //名字为user80,年龄为48岁
        bulkWriteOperation.find(new BasicDBObject("name","user80").append("age",48))
                .update(new BasicDBObject().append("$set",new BasicDBObject("flag",true)));
        //小于40岁
        bulkWriteOperation.find(new BasicDBObject().append("age",new BasicDBObject().append(QueryOperators.LTE,40)))
                .update(new BasicDBObject().append("$set",new BasicDBObject("flag",true)));

        BulkWriteResult result = bulkWriteOperation.execute();
        int matchedCount = result.getMatchedCount();
        int modifiedCount = result.getModifiedCount();

        System.out.println(matchedCount);
        System.out.println(modifiedCount);
    }

    /**
     * 批量删除
     */
    public void bulkRemove(){
        BulkWriteOperation bulkWriteOperation = getOrderedBulkOperation();
        //名字为user80,年龄为48岁
        bulkWriteOperation.find(new BasicDBObject("name","user80").append("age",48))
                .remove();
        //小于40岁
        bulkWriteOperation.find(new BasicDBObject().append("age",new BasicDBObject().append(QueryOperators.LTE,40)))
                .removeOne();

        BulkWriteResult result = bulkWriteOperation.execute();
        int matchedCount = result.getMatchedCount();
        int modifiedCount = result.getModifiedCount();
        int removeCount = result.getRemovedCount();

        System.out.println(matchedCount);
        System.out.println(modifiedCount);
        System.out.println(removeCount);
    }

    /**
     * 批量替换
     * 替换查询出来的一个文档。整个替换
     */
    public void bulkReplace(){
        BulkWriteOperation bulkWriteOperation = getOrderedBulkOperation();
        bulkWriteOperation.find(new BasicDBObject("name","user0"))
                .replaceOne(new BasicDBObject().append("name","Niki")
                        .append("age",27)
                        .append("address","南山区")
                        .append("hobby","篮球，美女")
                );

        BulkWriteResult result = bulkWriteOperation.execute();
        int matchedCount = result.getMatchedCount();
        int modifiedCount = result.getModifiedCount();
        System.out.println(matchedCount);
        System.out.println(modifiedCount);
    }
}
